﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fatec.ControleEstoque.Security.Providers;
using Fatec.ControleEstoque.Data.Model;

namespace Fatec.ControleEstoque.Interface
{
    public partial class Menu1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RemoverItensSemAcessoDoUsuario(HttpContext.Current.User.Identity.Name);
            }
        }

        protected void RemoverItensSemAcessoDoUsuario(String userName)
        {
            if (String.IsNullOrEmpty(userName))
            {
                this.menu.Items.Clear();
                return;
            }
            CustomRoleProvider crp = new CustomRoleProvider();
            String[] roles = crp.GetRolesForUser(userName);
            ListItemCollection itensARemover = new ListItemCollection();
            foreach(ListItem l in this.menu.Items)
            {
                if(l.Value.Split(':').Length > 1)
                {
                    String role = l.Value.Split(':')[1];
                    if (!roles.Contains(role))
                    {
                        itensARemover.Add(l);
                    }
                }                
            }
            foreach (ListItem l in itensARemover)
            {
                this.menu.Items.Remove(l);
            }
        }
    }
}